package com.library.web;
import java.util.*;
import org.apache.commons.lang3.StringUtils;


import com.library.entity.Seat;
import com.library.entity.Student;
import com.library.service.SeatService;

import my.bean.Msg;
import my.db.Page;
import my.mvc.Annotation.JSON;
import my.mvc.RequestContext;

@SuppressWarnings("unchecked")
public class SeatAction {

	private SeatService service = new SeatService();

	/**
	 * 列表   后台管理所用
	 */
	@JSON
	public Page<Seat> list(RequestContext rc) {
		// 获得分页信息, layui表格分页参数：page、limit
		Page<Seat> page = rc.form(Page.class);
		String campus = rc.param("campus");
		service.list(page, campus);
		return page;
	}
	/**
	 * 列表   前台个人预约信息页面 所用
	 */
	@JSON
	public Page<Seat> listPerson(RequestContext rc) {
		// 获得分页信息, layui表格分页参数：page、limit
		Page<Seat> page = rc.form(Page.class);
		Student student = (Student) rc.sessionAttr("student");
		String name=student.getName();
//		System.out.println(name);
		service.listPerson(page,name);
		return page;
	}
		
	

	/**
	 * 保存或修改
	 */
	@JSON
	public Msg save(RequestContext rc) throws Exception {
		Seat a = rc.form(Seat.class);
		if (a.getId() == 0) {
			service.save(a);
		} else {
			service.update(a);
		}
		return new Msg(true);
	}
	/**
	 * 保存
	 */
	@JSON
	public Msg save1(RequestContext rc) throws Exception {
		Seat a = rc.form(Seat.class);
		if (a.getId() == 0) {
			service.save(a);
		} else {
			service.update1(a);
		}
		return new Msg(true);
	}
	/*用户占座后修改座位信息
	 * 
	 * */
	@JSON
	public void update(RequestContext rc) throws Exception {
		Seat a = rc.form(Seat.class);
//		Student student = (Student) rc.sessionAttr("student");
//		String xm=student.getName();
//		System.out.println(xm);
//		List<Seat> seat=service.getByXm(xm);
//		if(seat!=null) {
//			rc.reqAttr("msg","您已选座，请勿重复选座");
//			rc.redirect("/page/pmsg/pmsg_view.jsp");
//		}
//		else {
			service.update1(a);
			
//		}
	}
	
	
	/*
	 * 用在
	 * */
	@JSON
	public void update1(RequestContext rc) throws Exception{
		long id = rc.param("id", 0L);
		Seat seat = service.get(id);
		rc.reqAttr("seat", seat);
		rc.forward("/page/seat/seat_form.jsp");
	}
	/**
	 * 后端修改信息时先获得信息
	 */
	@JSON
	public void get(RequestContext rc) {
		long id = rc.param("id", 0L);
		Seat seat = service.get(id);
		rc.reqAttr("seat", seat);
		rc.forward("/page/seat/seat_form.jsp");
	}
	/**
	 * 前端修改信息时先获得信息
	 */
	@JSON
	public void get1(RequestContext rc) {
		long id = rc.param("id", 0L);
		Seat seat = service.get(id);
		rc.reqAttr("seat", seat);
		rc.forward("/page/pmsg/pmsg_form.jsp");
	}
	/*
	 * 获得一个教室的座位信息
	 * 
	 * */
	@JSON
	public void getSeats(RequestContext rc) {
		int classroom = rc.param("classroom",101);
		List<Seat> seat=service.getSeats(classroom);
		rc.reqAttr("seat", seat);
		rc.forward("/page/library/select_form.jsp");
	}
	/**
	 * 删除(后端)
	 * * */
	@JSON
	public Msg multiDel(RequestContext rc) {
		String ids = rc.param("ids");
		if(StringUtils.isNotBlank(ids)) {
			String[] arr = ids.split(",");
			for(String id : arr) {
				service.delete(Long.parseLong(id));
			}
		}
		return new Msg();
	}
}
